# Maintainer: Justin Kromlinger <hashworks@archlinux.org>
# Contributor: Hao Long <imlonghao@archlinuxcn.org>

pkgname=trivy
pkgver=0.47.0
pkgrel=1
pkgdesc='A Simple and Comprehensive Vulnerability Scanner for Containers, Suitable for CI'
arch=('loong64' 'x86_64')
url='https://github.com/aquasecurity/trivy'
license=('Apache')
depends=('glibc')
optdepends=('rpm: RHEL/CentOS based image support')
makedepends=('go' 'btrfs-progs')
provides=('trivy')
conflicts=('trivy')
source=("${pkgname}-${pkgver}.tar.gz::${url}/archive/v${pkgver}.tar.gz")
b2sums=('ad199ea751ac6f237b373e692d4fe835f96b67c5eacefdc12665a2f64cb50821e576c2ff399fd5f91d18ce2b80cc1f3667dbddf0cb2e303fd5b3d453976bf7bb')

prepare() {
  cd ${pkgname}-${pkgver}

  find docs -type f -name "*.md" \
    -exec sed "s|\(../\)*imgs/\(.*\.[a-z]\{3\}\)|${url}/raw/main/docs/imgs/\2|g" -i {} \;

  rm -Rf docs/{build,imgs}
}

build() {
  cd ${pkgname}-${pkgver}/cmd/trivy

  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOFLAGS="-buildmode=pie -trimpath -mod=readonly -modcacherw"
  go build -ldflags "-linkmode=external -s -w -X=main.version=${pkgver}" .
}

package() {
  cd ${pkgname}-${pkgver}

  install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  install -Dm755 "cmd/trivy/${pkgname}" "${pkgdir}/usr/bin/${pkgname}"

  "$pkgdir/usr/bin/${pkgname}" completion bash | install -Dm644 /dev/stdin "$pkgdir/usr/share/bash-completion/completions/${pkgname}"
  "$pkgdir/usr/bin/${pkgname}" completion zsh | install -Dm644 /dev/stdin "$pkgdir/usr/share/zsh/site-functions/_${pkgname}"
  "$pkgdir/usr/bin/${pkgname}" completion fish | install -Dm644 /dev/stdin "$pkgdir/usr/share/fish/vendor_completions.d/${pkgname}.fish"

  mkdir -p "${pkgdir}/usr/share/doc"
  cp -r docs "${pkgdir}/usr/share/doc/${pkgname}"
}
